package com.rongc.client.core.utils;

import android.content.Context;

import com.rongc.client.freight.base.model.DicsBean;
import com.rongc.client.freight.base.model.TipListBean;
import com.rongc.client.freight.base.model.UserBean;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class FileManager {
	private static String USER_LOCAL = "User";
	private static String DICS_LOCAL = "DICS";
	private static String TIPS_LOCAL = "TIPS";

	public synchronized static boolean saveData(Context context, Object data,String name) {
		String filename = getDataFileName(data.getClass(),name);
		try {
			context.deleteFile(filename);
			FileOutputStream f = context.openFileOutput(filename, Context.MODE_PRIVATE);
			ObjectOutputStream obj_out = new ObjectOutputStream(f);
			obj_out.writeObject(data);
			obj_out.close();
			f.close();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	@SuppressWarnings("unchecked")
	public synchronized static <T> T loadData(Context context, Class<T> clz,String name) {
		String filename = getDataFileName(clz,name);
		try {
			FileInputStream f = context.openFileInput(filename);
			ObjectInputStream obj_in = new ObjectInputStream(f);
			T data = (T)obj_in.readObject();
			obj_in.close();
			f.close();
			return data;
		} catch (Exception e) {
			//e.printStackTrace();
		}
		return null;
	}

	public synchronized static void clearData(Context context, Class<?> clz,String name) {
		String filename = getDataFileName(clz,name);
		boolean b = context.deleteFile(filename);
	}

	private static String getDataFileName(Class<?> clz,String filename) {
		return getDataFileName(clz)+filename;
	}

	public synchronized static boolean saveData(Context context, Object data) {
		String filename = getDataFileName(data.getClass());
		try {
			context.deleteFile(filename);
			FileOutputStream f = context.openFileOutput(filename, Context.MODE_PRIVATE);
			ObjectOutputStream obj_out = new ObjectOutputStream(f);
			obj_out.writeObject(data);
			obj_out.close();
			f.close();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	@SuppressWarnings("unchecked")
	public synchronized static <T> T loadData(Context context, Class<T> clz) {
		String filename = getDataFileName(clz);
		try {
			FileInputStream f = context.openFileInput(filename);
			ObjectInputStream obj_in = new ObjectInputStream(f);
			T data = (T)obj_in.readObject();
			obj_in.close();
			f.close();
			return data;
		} catch (Exception e) {
			//e.printStackTrace();
		}
		return null;
	}

	public synchronized static void clearData(Context context, Class<?> clz) {
		String filename = getDataFileName(clz);
		boolean b = context.deleteFile(filename);
	}

	private static String getDataFileName(Class<?> clz) {
		if(UserBean.class.equals(clz)){
			return USER_LOCAL;
		}else if(DicsBean.class.equals(clz)){
			return DICS_LOCAL;
		}else if(TipListBean.class.equals(clz)){
			return TIPS_LOCAL;
		}
		return null;
	}

	public synchronized static boolean saveFile(Context context, String filename, InputStream in) {
		if(null == filename || filename.equals("")) {
			return false;
		}

		try {
			context.deleteFile(filename);
			FileOutputStream f;
			f = context.openFileOutput(filename, Context.MODE_PRIVATE);

			byte buf[] = new byte[1024];
			int numread;
			while((numread = in.read(buf)) != -1){
				f.write(buf, 0, numread);
			}
			f.close();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public synchronized static InputStream loadFile(Context context, String filename) {
		try {
			return context.openFileInput(filename);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
